<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
<#assign classNameAllLower = className?lower_case>
package ${basePackage}.${moduleName}.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import ${basePackage}.${moduleName}.model.entity.${className};
import ${basePackage}.${moduleName}.model.response.${className}VO;
import ${basePackage}.${moduleName}.service.${className}Service;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
<#if appHasAdd == 'true' || appHasEdit == 'true'>
import ${basePackage}.${moduleName}.model.request.${className}Form;
import org.springframework.validation.annotation.Validated;
</#if>

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-${table.remarks}管理", tags = "${table.remarks}管理")
@RestController("app${className}Controller")
@RequestMapping("${moduleName}/app/${classNameLower}")
public class ${className}Controller {

    @Autowired
    private ${className}Service ${classNameLower}Service;
    @Autowired
    private LocaleMessage localeMessage;

    <#if appHasDetail == 'true'>
    @GetMapping()
    public PageResult<${className}VO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
        <#if appSearchField?? && appSearchField != "">
            <#list appSearchField?split(",") as field>
                <#list table.columns as column>
                    <#if column.sqlName == field>
                        <#if column.isEnum>
        , @RequestParam(name = "${column.columnNameLower}", value = "${column.columnAlias}", required = false) ${className}.${column.columnName} ${column.columnNameLower}
                        <#elseif column.isDateTimeColumn>
        , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss" ) @RequestParam(name = "start${column.columnName}", value = "开始${column.columnAlias}", required = false) ${column.simpleJavaType} start${column.columnName}
        , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss" ) @RequestParam(name = "end${column.columnName}", value = "截至${column.columnAlias}", required = false) ${column.simpleJavaType} end${column.columnName}
                        <#else>
        , @RequestParam(name = "${column.columnNameLower}", value = "${column.columnAlias}", required = false) ${column.simpleJavaType} ${column.columnNameLower}
                        </#if>
                        <#break>
                    </#if>
                </#list>
            </#list>
        </#if>
    ) {
        Page<${className}VO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        <#if appSearchField?? && appSearchField != "">

        //搜索
            <#list appSearchField?split(",") as field>
                <#list table.columns as column>
                    <#if column.sqlName == field>
                        <#if column.isDateTimeColumn>
        if (start${column.columnName} != null) {
            params.put("${column.columnNameLower}", start${column.columnName});
        }
        if (end${column.columnName} != null) {
            params.put("${column.columnNameLower}", end${column.columnName});
        }
                        <#elseif column.isEnum>
        if (${column.columnNameLower} !=null){
            params.put("${column.columnNameLower}", ${column.columnNameLower}.getValue().toString());
        }
                        <#elseif column.simpleJavaType == "String">
        if (StringUtils.isNotBlank(${column.columnNameLower})) {
            params.put("${column.columnNameLower}", ${column.columnNameLower});
        }
                        <#else>
        if (${column.columnNameLower} !=null){
            params.put("${column.columnNameLower}", ${column.columnNameLower});
        }
                        </#if>
                        <#break>
                    </#if>
                </#list>
            </#list>
        </#if>
        <#list table.columns as column>
            <#if column.sqlName == "member_id">

        params.put("memberId", member.getId());
                <#break>
            </#if>
        </#list>

        Page<${className}VO> list = ${classNameLower}Service.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    </#if>
    <#if appHasDetail == 'true'>
    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long ${classNameLower}Id) {
        ${className}VO ${classNameLower}VO = ${classNameLower}Service.get4appById(${classNameLower}Id);
        if (${classNameLower}VO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), ${classNameLower}VO);
    }

    </#if>
    <#if appHasAdd == 'true'>
    @PostMapping()
    public JsonResult add(@Validated ${className}Form ${classNameLower}Form) {
        if (${classNameLower}Service.add(${classNameLower}Form) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    </#if>
    <#if appHasEdit == 'true'>
    @PutMapping()
    public JsonResult update(${className}Form ${classNameLower}Form) {
        ${className} ${classNameLower} =
            ${classNameLower}Service.getOne(new QueryWrapper<${className}>().eq("id" , ${classNameLower}Form.getId()));
        if (${classNameLower} ==null){
            return JsonResult.fail(localeMessage.getMessage("common.fail" ));
        }
        if (${classNameLower}Service.updateById(${classNameLower})) {
            return JsonResult.success(localeMessage.getMessage("common.success" ));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail" ));
    }

    </#if>
    <#if appHasDelete == 'true'>
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long ${classNameLower}Id) {
        ${className} ${classNameLower} =${classNameLower}Service.getById(${classNameLower}Id);
        if (${classNameLower} ==null){
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        if (${classNameLower}Service.removeById(${classNameLower}Id)) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    </#if>
}
